﻿using System;

namespace HslCommunication.ModBus
{
	/// <summary>
	/// Modbus-Ascii通讯协议的类库，基于rtu类库完善过来，支持标准的功能码，也支持扩展的功能码实现，地址采用富文本的形式，详细见备注说明<br />
	/// The client communication class of Modbus-Ascii protocol is convenient for data interaction with the server. It supports standard function codes and also supports extended function codes. 
	/// The address is in rich text. For details, see the remarks.
	/// </summary>
	/// <remarks>
	/// 本客户端支持的标准的modbus协议，Modbus-Tcp及Modbus-Udp内置的消息号会进行自增，地址支持富文本格式，具体参考示例代码。<br />
	/// 读取线圈，输入线圈，寄存器，输入寄存器的方法中的读取长度对商业授权用户不限制，内部自动切割读取，结果合并。
	/// </remarks>
	/// <example>
	/// 基本的用法请参照下面的代码示例，初始化部分的代码省略
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Modbus\ModbusAsciiExample.cs" region="Example" title="Modbus示例" />
	/// 复杂的读取数据的代码示例如下：
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Modbus\ModbusAsciiExample.cs" region="ReadExample" title="read示例" />
	/// 写入数据的代码如下：
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Modbus\ModbusAsciiExample.cs" region="WriteExample" title="write示例" />
	/// </example>
	// Token: 0x020000CC RID: 204
	public class ModbusAscii : ModbusRtu
	{
		/// <summary>
		/// 实例化一个Modbus-ascii协议的客户端对象<br />
		/// Instantiate a client object of the Modbus-ascii protocol
		/// </summary>
		// Token: 0x06000F53 RID: 3923 RVA: 0x00059CBE File Offset: 0x00057EBE
		public ModbusAscii()
		{
			this.LogMsgFormatBinary = false;
		}

		/// <inheritdoc cref="M:HslCommunication.ModBus.ModbusRtu.#ctor(System.Byte)" />
		// Token: 0x06000F54 RID: 3924 RVA: 0x00059CCF File Offset: 0x00057ECF
		public ModbusAscii(byte station = 1) : base(station)
		{
			this.LogMsgFormatBinary = false;
		}

		/// <inheritdoc />
		// Token: 0x06000F55 RID: 3925 RVA: 0x00059CE4 File Offset: 0x00057EE4
		protected override OperateResult<byte[]> CheckModbusTcpResponse(byte[] send)
		{
			byte[] send2 = ModbusInfo.TransModbusCoreToAsciiPackCommand(send);
			OperateResult<byte[]> operateResult = base.ReadBase(send2);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = ModbusInfo.TransAsciiPackCommandToCore(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = send[1] + 128 == operateResult2.Content[1];
					if (flag3)
					{
						result = new OperateResult<byte[]>((int)operateResult2.Content[2], ModbusInfo.GetDescriptionByErrorCode(operateResult2.Content[2]));
					}
					else
					{
						result = ModbusInfo.ExtractActualData(operateResult2.Content);
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000F56 RID: 3926 RVA: 0x00059D7B File Offset: 0x00057F7B
		public override string ToString()
		{
			return string.Format("ModbusAscii[{0}:{1}]", base.PortName, base.BaudRate);
		}
	}
}
